package com.begye.smartloan.apptest.tool

import com.begye.smartloan.apptest.MyApplication
import org.json.JSONObject

object LoginTool {

    var didCertify: Boolean = false

    fun updateLoginInfo(jsonObject: JSONObject) {
        val tk = jsonObject.optString("token", "")
        val uid = jsonObject.optString("userId", "")
        if (tk.isNotBlank()) {
            MyApplication.instance.initLibs(tk, uid)
        }
        SpTool.save(LoginTool::class.java.simpleName, jsonObject.toString())
    }

    fun filterAdType(): Boolean {
        return try {
            val text = SpTool.get(LoginTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            val source = jsonObject.optString("isAdSource", "NO")
            source == "YES"
        } catch (e: Exception) {
            false
        }
    }

    fun filterSession(): String {
        return try {
            val text = SpTool.get(LoginTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            jsonObject.optString("token")
        } catch (e: Exception) {
            ""
        }
    }

    fun filterUID(): String {
        return try {
            val text = SpTool.get(LoginTool::class.java.simpleName)
            val jsonObject = JSONObject(text)
            jsonObject.optString("userId")
        } catch (e: Exception) {
            //
            ""
        }
    }


}